import { Component, Fragment, Dispatch } from 'react'
import { View, Image, Text } from '@tarojs/components'
import Global from '@/src/lib/global'
import Taro from '@tarojs/taro'
import './addressNav.scss'
import { connect } from 'react-redux'
// import { State } from '@/src/dva/state'

interface AddressNavProps {
    dispatch: Dispatch<any>
    isAdd: string,
    nav?:boolean
}

interface AddressNavState {


}


class AddressNav extends Component<AddressNavProps, AddressNavState> {
    constructor(props) {
        super(props)
        this.state = {
            // isAdd:props.isAdd,
        }
        console.log('props====', props)

    }
    // getDerivedStateFromProps(props){
    //     console.log('props====',props)
    //     this.setState({
    //         isAdd:props.isAdd
    //     })
    // }
    getAddress = () => {
        let address = {
            name: '',//姓名
            city: '',//省市区(县) 格式：广东省/深圳市/南山区
            phone: '',//电话号码
            particular: '',//详细地址
            swicth: false,//默认地址
        };
        let that = this;
        Taro.chooseAddress({
            success: res => {
                console.log(res)
                address.name = res.userName;
                address.city = `${res.provinceName}/${res.cityName}/${res.countyName}`;
                address.phone = res.telNumber;
                address.particular = res.detailInfo;
                if (that.props.isAdd === '1') {
                    that.props.dispatch({
                        type: "address/setaddress",
                        payload: address
                    })
                  if(that.props.nav){
                    Taro.redirectTo({
                        url:'/pages/subpackages/pages/affirmOrder/index'
                    })
                  }
                } else if (that.props.isAdd === '0') {
                    that.props.dispatch({
                        type: "address/setamendAddress",
                        payload: address
                    })
                } else {
                    return
                }

                // 跳转到下单页面
                // Taro.redirectTo({
                //     url:'/pages/'
                // })
            }

        }).catch(error => {
            console.log(error)
        })
    }
    render() {
        return (
            <Fragment>
                <View className="addTop" onClick={this.getAddress}>
                    <View className="topLeft">
                        <Image src={Global.loadImage('weixin@3x.png')} className="wxPng"></Image>
                        <Text className="addTexte"> 获取微信地址</Text>
                    </View>
                    <View className="topRig dis" >
                        <Image src={Global.loadImage('icon_arrow_right@3x.png')} className="rigTopPng"></Image>
                    </View>

                </View>
            </Fragment>
        )
    }
}
export default connect(() => {
    return {}
}, (dispatch) => ({ dispatch }))(AddressNav)